//g++ omp.cpp -fopenmp
#include<iostream>
#include<omp.h>
#include<stdio.h>
#include<ctime>
using namespace std;

void test(){
	int a = 0;
    clock_t start = clock();
	for(int i = 0;i<100000000;i++){
	a = a+i;
	}
	clock_t end = clock();
	printf("Time = %d \n",end-start);	
}

int main()
{
	clock_t start,end;
	int n = 100000;
	int sum = 0;
	omp_set_num_threads(4);
	start = clock();
//#pragma omp parallel
	{
//#pragma omp for reduction(+:sum)
		for(int i = 0;i<n;i++){
//#pragma omp critical
			sum +=i+1;
		}
	}
	end = clock();
	cout<<"sum= "<< sum<<endl<<end-start<<endl; 
/*
 * teset 3
	int sum = 0 ;
	int a[10]={1,2,3,4,5,6,7,8,9,10};
	int coreNum = omp_get_num_procs();
	cout<< coreNum<<endl;
	int* sumArray = new int[coreNum];
	for(int i =0;i<coreNum;++i)
		sumArray[i]=0;
#pragma omp parallel for
	for(int i=0;i<10;i++){
		int k = omp_get_thread_num();
		sumArray[k]=sumArray[k]+a[i];
	}
	for(int i=0;i<coreNum;++i)
		sum = sum + sumArray[i];
	cout<<"sum: "<<sum <<endl;
*/
/*test2	
	clock_t start = clock();
	#pragma omp parallel for
		for(int j =0;j<3;j++){
			test();
		}
	clock_t end = clock();
	printf("Totala time %d \n.",end-start);
	test();
*/
/*
// test1
     for (int i = 0; i < 10; i++ )
     {
	printf("i= %d \n",i);
//         cout<<"i = "<<i<<endl;
     }
*/
     return 0;
}
